import 'package:flutter/material.dart';
import 'package:flutter_redux/flutter_redux.dart';
import 'package:redux/redux.dart';
import 'package:xlp_github_app_flutter/common/style/app_style.dart';
import 'package:xlp_github_app_flutter/common/utils/common_utils.dart';
import 'package:xlp_github_app_flutter/common/utils/navigator_utils.dart';
import 'package:xlp_github_app_flutter/model/User.dart';
import 'package:xlp_github_app_flutter/page/MainPage.dart';
import 'package:xlp_github_app_flutter/page/WelcomePage.dart';
import 'package:xlp_github_app_flutter/redux/app_state.dart';

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  final store = Store<AppState>(appReducer,
      initialState: AppState(
          userInfo: User.empty(),
          login: false,
          themeData: CommonUtils.getThemeData(AppColors.primarySwatch)));

  NavigatorObserver navigatorObserver = NavigatorObserver();

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return StoreProvider(
      store: store,
      child: StoreBuilder<AppState>(builder: (context, store) {
        MaterialApp app = MaterialApp(
          theme: store.state.themeData,
          routes: {
            WelcomePage.sName: (context) {
              return const WelcomePage();
            },
            MainPage.sName: (context) {
              return NavigatorUtils.pageContainer(const MainPage(), context);
            }
          },
        );

        return app;
      }),
    );
  }
}
